package com.example.function_interface.function;

import com.example.function_interface.User;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

/**
 * 代表了一个接受两个输入参数的方法，并且返回一个结果
 */
public class BiFunctionTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", 12, false));
        userList.add(new User(2, "李四", 22, false));
        userList.add(new User(3, "王五", 32, true));
        userList.add(new User(4, "郑静", 2, true));

        BiFunction<User, Integer, String> biFunction = new BiFunction() {
            @Override
            public Object apply(Object o, Object o2) {
                User user = (User) o;
                Integer age = (Integer) o2;
                if (user.getAge() >= age) {
                    return user.getName();
                }
                return null;
            }
        };
        BiFunction<User, Integer, Object> comboBiFunction = biFunction.andThen((name) -> {
            if (StringUtils.isNotBlank(name)) {
                return "["+name+"],";
            }
            return "";
        });

        for (User user : userList) {
            String name = (String) comboBiFunction.apply(user, 18);
            System.out.print(name);
        }

    }
}
